<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{

    use Authenticatable, CanResetPassword;

    use SoftDeletes;

    protected $table = 'users';

    protected $fillable = ['name', 'email', 'gender', 'phone', 'address', 'password', 'role_id'];

    protected $hidden = ['password', 'remember_token'];

    protected $dates = ['deleted_at'];

    public static $_gender = ['male' => '男', 'female' => '女'];


    public function isAdmin()
    {
        if ($this->role) {
            return $this->role->isAdmin();
        }
        return false;
    }

    public function role()
    {
        return $this->belongsTo('App\Models\Role', 'role_id', 'id');
    }

    public function findItems()
    {
        return $this->items->where('type', '=', 'find')->all();
    }

    public function lostItems()
    {
        return $this->items->where('type', '=', 'lost')->all();
    }

    public function items()
    {
        return $this->hasMany('App\Models\Item', 'id', 'user_id');
    }
}
